#include"boxgame.h"
#include"memory.h"
boxgame::boxgame()
{
    initmap();
}
void boxgame::initmap()//对地图的初始化
{
    int map1[3][8][8] =
    {
        1,1,1,1,1,1,1,1,
        1,3,4,0,0,0,0,1,
        1,0,1,0,1,1,0,1,
        1,0,0,5,0,0,0,1,
        1,0,1,0,3,1,0,1,
        1,0,1,0,4,1,0,1,
        1,0,0,0,0,4,3,1,
        1,1,1,1,1,1,1,1,

        1,1,1,1,1,1,1,1,
        1,1,1,1,1,1,3,1,
        1,0,1,1,5,0,3,1,
        1,0,1,1,4,4,3,1,
        1,0,0,0,0,4,0,1,
        1,0,4,4,0,0,0,1,
        1,3,3,1,1,1,1,1,
        1,1,1,1,1,1,1,1,

        1,1,1,1,1,1,1,1,
        1,1,1,1,1,1,1,1,
        1,3,0,3,0,3,1,1,
        1,0,4,4,4,0,1,1,
        1,3,4,5,4,3,1,1,
        1,0,4,4,4,0,1,1,
        1,3,0,3,0,3,1,1,
        1,1,1,1,1,1,1,1,
    };
    memcpy(&map[0][0][0],&map1[0][0][0],sizeof(int)*64*3);//利用拷贝的方式初始化地图
}
void boxgame::drawmap(QPainter* pdraw)//绘画地图
{
     QString imageURL;
    for (int i = 0; i < 8; i++)
    {
         for (int j = 0; j < 8; j++) {
            switch (map[cas][i][j])
            {
            case 0:
                 imageURL="://image/0.png";
                break;
            case 1:
                imageURL="://image/1.png";
                break;
            case 3:
                imageURL="://image/3.png";
                break;
            case 4:
                imageURL="://image/4.png";
                break;
            case 5:
            case 8:
                 imageURL="://image/5.png";
                break;
            case 7:
                imageURL="://image/7.png";
                break;
            }//贴图
            QImage image(imageURL);
            pdraw->drawImage(QRect(j*image.width(),i*image.height(),image.width(),image.height()),image);
        }
    }
}
void boxgame::keydown(QKeyEvent* event)//获取用户键盘上的输入
{
        int i = getrow();
        int j = getcols();
        switch (event->key())
        {
        case Qt::Key_W:
        case Qt::Key_Up:
            if (map[cas][i - 1][j] == 0 || map[cas][i - 1][j] == 3)
            {
                map[cas][i][j] -= 5;
                map[cas][i - 1][j] += 5;
            }
            if (map[cas][i - 1][j] == 4 || map[cas][i - 1][j] == 7)
            {
                if (map[cas][i - 2][j] == 0 || map[cas][i - 2][j] == 3)
                {
                    map[cas][i][j] -= 5;
                    map[cas][i - 1][j] += 1;
                    map[cas][i - 2][j] += 4;
                }
            }
            break;
        case Qt::Key_S:
        case Qt::Key_Down:
            if (map[cas][i + 1][j] == 0 || map[cas][i + 1][j] == 3)
            {
                map[cas][i][j] -= 5;
                map[cas][i + 1][j] += 5;
            }
            if (map[cas][i + 1][j] == 4 || map[cas][i + 1][j] == 7)
            {
                if (map[cas][i + 2][j] == 0 || map[cas][i + 2][j] == 3)
              {
                    map[cas][i][j] -= 5;
                    map[cas][i + 1][j] += 1;
                    map[cas][i + 2][j] += 4;
                }
            }
            break;
        case Qt::Key_A:
        case Qt::Key_Left:
            if (map[cas][i][j - 1] == 0 || map[cas][i][j - 1] == 3)
            {
                map[cas][i][j] -= 5;
                map[cas][i][j - 1] += 5;
            }
            if (map[cas][i][j - 1] == 4 || map[cas][i][j - 1] == 7)
            {
                if (map[cas][i][j - 2] == 0 || map[cas][i][j - 2] == 3)
                {
                    map[cas][i][j] -= 5;
                    map[cas][i][j - 1] += 1;
                    map[cas][i][j - 2] += 4;
                }
            }
            break;
        case Qt::Key_D:
        case Qt::Key_Right:
            if (map[cas][i][j + 1] == 0 || map[cas][i][j + 1] == 3)
            {
                map[cas][i][j] -= 5;
                map[cas][i][j + 1] += 5;
            }
            if (map[cas][i][j + 1] == 4 || map[cas][i][j + 1] == 7)
            {
                if (map[cas][i][j + 2] == 0 || map[cas][i][j + 2] == 3)
                {
                    map[cas][i][j] -= 5;
                    map[cas][i][j + 1] += 1;
                    map[cas][i][j + 2] += 4;
                }
            }
            break;
        }
}
int boxgame::getrow()//获取人物行坐标
{
    for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                if (map[cas][i][j] == 5 || map[cas][i][j] == 8)
                    return i;
            }
        }
        return -1;
}
int boxgame::getcols()//获取人物列坐标
{
    for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                if (map[cas][i][j] == 5 || map[cas][i][j] == 8)
                    return j;
            }
        }
        return -1;
}
bool boxgame::gameover()
{
    for(int i=0;i<8;i++)
    {
        for(int j=0;j<8;j++)
        {
            if(map[cas][i][j]==4)
            {
                return false;
            }
        }
    }
    if(cas==2){
    return true;
    }
    if(cas<2){
        cas++;
        return false;
    }
}
